<template>
  <div>
    <van-nav-bar
      title="收货地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      fixed
    />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="onSelect"
      style="margin-top: 1rem"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      chosenAddressId: "0",
      list:[],
    };
  },
  methods: {
    onClickLeft() {
      this.$router.push('/own');
    },
    	// 点击新增按钮时触发
    onAdd() {
      this.$router.push("/addressEdit");
    },
    onEdit(item, index) {
      Toast("编辑地址:" + index);
    },
  //  切换选中的地址时触发
    onSelect(item,index){
    console.log(item,index);
    this.$store.commit('muSelectAddress',item)
  }
  },
  // 创建时获取所有的地址信息进行渲染
  created() {
    let list = [];
    let address_info = this.$store.state.address_info;
    address_info.forEach((item, index) => {
      list.push({
        id: index,
        name: item.name,
        tel: item.tel,
        address:item.city+item.county+item.addressDetail ,
        isDefault: item.isDefault,
      });
    });
    this.list=list
  },
};
</script>

<style>
</style>